# import time
#
# import pytest
# from selenium import webdriver
# from selenium.webdriver.chrome.service import Service
# from selenium.webdriver.common.by import By
#
# def get_driver():
#         service = Service(executable_path=r'D:\FlowerRecognition\auto_1\chromedriver.exe')
#         driver = webdriver.Chrome(service=service)
#         driver.get("http://localhost:90")
#         driver.maximize_window()
#         return driver
#
# def login(username, password,expect):
#         driver = get_driver()
#         driver.find_element(By.ID, "username").send_keys(username)
#         driver.find_element(By.ID, "password").send_keys(password)
#         # WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "login_btn"))).click()
#         driver.find_element(By.ID, "login_btn").click()
#         driver.implicitly_wait(5)
#         return driver
#
#
# def register(username,password,repassword,expect):
#         driver = get_driver()
#         driver.find_element(By.ID, "username").send_keys(username)
#         driver.find_element(By.ID, "password").send_keys(password)
#         driver.find_element(By.ID,"repassword").send_keys(repassword)
#         driver.find_element(By.ID, "register_btn").click()
#         driver.implicitly_wait(5)
#         return driver

import hashlib
import time

import pymysql
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

host="localhost"
port=3306
user="root"
password="root"
database="finance"

def select(sql,param):
    con = pymysql.connect(host=host, port=port, user=user, password=password, database=database)
    cur = con.cursor()
    cur.execute(sql,param)
    result = cur.fetchall()
    con.commit()
    cur.close()
    con.close()
    return result

def update(sql ,param):
    con = pymysql.connect(host=host, port=port, user=user, password=password, database=database)
    cur = con.cursor()
    cur.execute(sql,param)
    con.commit()
    cur.close()
    con.close()

def md5_hash(text: str) -> str:
    # 将字符串编码为字节（MD5需要bytes类型）
    text_bytes = text.encode('utf-8')
    # 创建MD5对象并更新数据
    md5_obj = hashlib.md5()
    md5_obj.update(text_bytes)
    # 获取16进制表示的哈希值
    return md5_obj.hexdigest()
def chaxun_user(name):
    sql = "select * from user where username = %s"
    param = [name]
    result = select(sql,param)
    return result
def insert_user(name,password):
    result = chaxun_user(name)
    md5_result = md5_hash(str(password))
    if len(result) < 1 and name!="admin":
        update("insert into user(username,password) values(%s,%s)", [name,md5_result])
def delete_user(name):
    update("delete from user where username = %s", [name])
def get_driver():
    service = Service(executable_path=r'D:\FlowerRecognition\auto_1\chromedriver.exe')
    driver = webdriver.Chrome(service=service)
    driver.get("http://localhost:90")
    driver.maximize_window()
    driver.implicitly_wait(2)
    return driver
def login(username, password):
    driver = get_driver()
    driver.find_element(By.ID, "username").send_keys(username)
    driver.find_element(By.ID, "password").send_keys(password)
    # WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "login_btn"))).click()
    driver.find_element(By.ID, "login_btn").click()
    # time.sleep(1)
    return driver
def register(username, password,repassword):
    driver = get_driver()
    driver.find_element(by=By.XPATH , value="/html/body/div/div/div/div[2]/a").click()
    time.sleep(1)
    wait = WebDriverWait(driver, 10)
    input=wait.until(EC.visibility_of_element_located((By.ID, "username")))
    input.send_keys(username)
    driver.find_element(By.ID, "password").send_keys(password)
    driver.find_element(By.ID, "repassword").send_keys(repassword)
    driver.find_element(By.ID, "login_btn").click()
    return driver
def accountsecurity(username, password,oldpwd,newpwd,confirmpwd,expected):
    driver = get_driver()
    driver.find_element(By.ID, "username").send_keys(username)
    driver.find_element(By.XPATH, "//*[@id='password']").send_keys(password)
    driver.find_element(By.ID, "login_btn").click()
    driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
    time.sleep(1)
    driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/ul/li[4]/a").click()
    time.sleep(1)
    driver.find_element(By.ID, "oldpwd").send_keys(oldpwd)
    driver.find_element(By.ID, "newpwd").send_keys(newpwd)
    driver.find_element(By.ID, "confirmpwd").send_keys(confirmpwd)
    driver.find_element(By.XPATH, "//*[@id='submit']").click()
    return driver
def credit_manage(username,password,creditmanage,number,expected):
    driver = get_driver()
    driver.find_element(By.ID, "username").send_keys(username)
    driver.find_element(By.XPATH, "//*[@id='password']").send_keys(password)
    driver.find_element(By.ID, "login_btn").click()
    driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
    time.sleep(1)
    driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
    time.sleep(1)
    driver.find_element(By.XPATH,"//*[@id='bankCard_add_modal_btn']").click()
    time.sleep(1)

    driver.find_element(By.ID, "cardBank_add_input").send_keys(creditmanage)
    time.sleep(1)
    driver.find_element(By.ID, "cardNum_add_input").send_keys(number)
    time.sleep(1)
    driver.find_element(By.XPATH, "//*[@id='bankCard_save_btn']").click()
    time.sleep(2)
    return driver

def fund_manage(username, password, payword, expected):
    driver = get_driver()
    driver.find_element(By.ID, "username").send_keys(username)
    driver.find_element(By.XPATH, "//*[@id='password']").send_keys(password)
    driver.find_element(By.ID, "login_btn").click()
    el4 = driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[2]/a")
    el4.click()
    time.sleep(1)
    # 点击基金理财
    el4 = driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[2]/ul/li[3]/a")
    el4.click()
    time.sleep(1)
    # 点击投资按钮
    el5 = driver.find_element(By.XPATH,
                              "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button")
    el5.click()
    time.sleep(0.5)
    # 输入支付密码
    el6 = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/input")
    el6.send_keys('666666')
    time.sleep(0.25)
    # 点击确定按钮
    el7 = driver.find_element(By.XPATH, "/html/body/div[3]/div[3]/a[1]")
    el7.click()
    time.sleep(0.5)
    # 点击确认按钮
    el8 = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[4]/button")
    el8.click()
    time.sleep(1)
    return driver

def apply_manage(username, password, money,term, expected):
    driver = get_driver()
    driver.find_element(By.ID, "username").send_keys(username)
    driver.find_element(By.XPATH, "//*[@id='password']").send_keys(password)
    driver.find_element(By.ID, "login_btn").click()
    time.sleep(1)
    driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[3]/a").click()
    time.sleep(1)
    driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[3]/ul/li[2]/a").click()
    time.sleep(1)
    driver.find_element(By.XPATH, "//*[@id='amount']").send_keys(money)
    time.sleep(1)
    driver.find_element(By.XPATH, "//*[@id='term']").send_keys(term)
    time.sleep(1)
    driver.find_element(By.XPATH, " //*[@id='submit']").click()
    time.sleep(1)
    driver.find_element(By.XPATH, " /html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
    time.sleep(1)
    return driver
    # 点击基金理财
    #     el4 = driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[2]/ul/li[3]/a")
    #     el4.click()
    #     time.sleep(1)
    #     # 点击投资按钮
    #     el5 = driver.find_element(By.XPATH,
    #                               "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button")
    #     el5.click()
    #     time.sleep(0.5)
    #     # 输入支付密码
    #     el6 = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/input")
    #     el6.send_keys('666666')
    #     time.sleep(0.25)
    #     # 点击确定按钮
    #     el7 = driver.find_element(By.XPATH, "/html/body/div[3]/div[3]/a[1]")
    #     el7.click()
    #     time.sleep(0.5)
    #     # 点击确认按钮
    #     el8 = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[4]/button")
    #     el8.click()
    #     time.sleep(1)


    # driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
    # time.sleep(1)
    # driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/ul/li[4]/a").click()
    # time.sleep(1)
    # driver.find_element(By.ID, "oldpwd").send_keys(oldpwd)
    # driver.find_element(By.ID, "newpwd").send_keys(newpwd)
    # driver.find_element(By.ID, "confirmpwd").send_keys(confirmpwd)
    # driver.find_element(By.XPATH, "//*[@id='submit']").click()
    # wait = WebDriverWait(driver, 10)
    #
    # input=wait.until(EC.visibility_of_element_located((By.ID, "username")))
    # input.send_keys(username)
    # #
    # driver.find_element(By.ID, "username").send_keys(username)
